
const USERNAME = localStorage.getItem('username');
// const TOKEN = localStorage.outlogin('token');
const TOKEN = localStorage.getItem('token');

//验证登录状态
(function(){
	
	let ologin = document.querySelector(".logo-btn");
	let oregistered = document.querySelector(".register-btn");
	let userinfo = document.querySelector(".user-wrapper");
	let userNAME = userinfo.querySelector("h1");
	let outLogin = document.querySelector(".outlogin");
	let carinfo = document.querySelector(".cart-info ul");
	let carbnt = document.querySelector(".look");
	let searchBnt = document.querySelector(".search>span");
	let searchInput = document.querySelector(".search>input");
	
	let LoginAlert = document.querySelector(".LoginAlert");
	let plogin = document.querySelector(".plogin");
	let cha = document.querySelector(".cha");
	console.log(plogin)
	cha.onclick = function(){
		LoginAlert.style.display = "none";
		plogin.style.display = "none";
	};
	
	if(USERNAME && TOKEN){
		loignStatus();
		carlist()
	}else{
		noLoginCar();
	}
	searchBnt.onclick = function(){
		console.log(searchInput.value.trim());
		json.get(URL + "/api_search",{
			page : 1,
			pagesize : 9,
			keywords : searchInput.value.trim(),
		},even =>{
			console.log(even)
			 if(even.code != 0){
				 return
			 }
			 location.href = `classify.html?keywords=${searchInput.value.trim()}&pagesize=${even.count}`;
		});
		
	}	
	function loignStatus(){
		
		 ologin.style.display = "none";
		 oregistered.style.display = "none";
		 userinfo.style.display = "block";
		 userNAME.innerHTML = USERNAME;
		 
		 outLogin.onclick = function(){
			 localStorage.removeItem('username');
			 localStorage.removeItem('token');
			 // location.href = 'login.html';
			 location.reload();
		 }
		 carbnt.onclick = function(){
			 location.href = 'car.html';
		 }	
		 
		 
	}
	
	//未登录购物车
	function noLoginCar(){
		carinfo.innerHTML = "<h3>亲您还未登录</h3>"
		carinfo.style.color = "white"
		carbnt.innerHTML = "去登录"
		carbnt.onclick = function(){
			location.href = 'login.html';
		}	
	}
	
	
  
})();
//渲染购物车
	function carlist(){
		let carinfo = document.querySelector(".cart-info ul");
		json.post(URL + '/api_cart',{
			status : 'viewcart',
			userId : TOKEN
		}, even =>{
			// console.log(even);
			if(even.code != 0){
				console.log(even);
				return;
			};
			
			//验证是否有商品
			let {data} = even; // res.data;
			if(data.length == 0){
				carinfo.innerHTML = '<h2>你的购物车暂时没有商品...</h2>';
				carinfo.style.color = "white"
				return;
			};
			
			let str = '';
			data.forEach(item => {
				str += `
					<li>
						<a> <img src="${item.goods_thumb}"> </a>
						<div>
							<p>${item.goods_name}</p>
							<p>${item.goods_desc}</p>
							<p><span>数量：${item.goods_number}件</span>
							<span>￥${item.price}</span></p>
						</div>
					</li>
				`;
			});			
			//添加到页面
			carinfo.innerHTML = str;		
			
		})
	}
//渲染分类导航
(function(){
	
	let oSubnavList = document.querySelector(".nav-goods-list-l ul")

	
	json.get(`${URL}/api_cat`,{},function(even){
	
		if(even.code != 0){
			return;
		}
		let str = "";
		even.data.forEach(function(item){
			
			str += `<li>
						<a href="classify.html?catId=${item.cat_id}&catName=${item.cat_name}">
						 <img src="${item.cat_img}">
						<p>${item.cat_name}</p>
						</a>
				</li>`
	
		});
		oSubnavList.innerHTML = str;
	})
	
})();